package com.b2b.controller;

import com.b2b.exception.CustomException;
import com.b2b.model.AdvertisementDto;
import com.b2b.model.BiddingDto;
import com.b2b.model.MessageDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.model.entity.Advertisement;
import com.b2b.util.AdvertisementUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.elasticsearch.common.util.ObjectArray;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/advertisement")
@PropertySource("classpath:config.properties")
public class AdvertisementController {
    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final AdvertisementUtil advertisementUtil;

    public AdvertisementController(AdvertisementUtil advertisementUtil){
        this.advertisementUtil = advertisementUtil;
    }


    @PostMapping
    public ResponseBean addAdvertisement(@RequestBody AdvertisementDto advertisementDto){
        Integer result = advertisementUtil.addAdvertisement(advertisementDto);
        if (result == -2){
            throw new CustomException("名称重复");
        }
        if (result <= 0){
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", result);
    }

    @PostMapping("/get")
    public ResponseBean getByList(@RequestBody Map<String,String[]> noListDto){
        String[] noList = noListDto.get("noList");
        List<Advertisement> advertisementList = advertisementUtil.getByNoList(noList);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Insert Success)", advertisementList);
    }
    @GetMapping("/getAll")
    public ResponseBean getAdvertisement(BaseDto baseDto){
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<Advertisement> advertisementList = advertisementUtil.selectAdvertisement();
        PageInfo<Advertisement> selectPage = new PageInfo<Advertisement>(advertisementList);
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("advertisementList", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Insert Success)", result);
    }

    @DeleteMapping("/delete")
    public ResponseBean deleteByAdvertisementId(@RequestParam Integer advertisementId){
        Integer result = advertisementUtil.deleteAdvertisement(advertisementId);

        if (result <= 0){
            throw new CustomException("删除失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Insert Success)", null);
    }

    @PutMapping("/update")
    public ResponseBean updateAdvertisement(@RequestBody AdvertisementDto advertisementDto){
        Integer result = advertisementUtil.updateAdvertisement(advertisementDto);

        if (result <= 0){
            throw new CustomException("删除失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Insert Success)", null);
    }
}
